/**
 * @author casazaft
 */
Package("j2j.effect").Class("Moveable", {
    Moveable: function(config){
    
    
    },
    Prototype: {
        extendController: function(config){
            var geo = Import("j2j.dom.Geometry");
            var emg = this.emg;
            var dom = config.dom || this.dom;
            var proxy = config.proxydom || this.select(config.proxy)[0] || dom;
            if (!dom) 
                throw Error(this.getClass().getName() + " no found dom!");
            var style = dom.style;
            var dx, dy, origX, origY;
			var _this = this;
            var onMouseDownHandler = function(e){
				_this.addCss("moveable");
                origX = dom.offsetLeft;
                origY = dom.offsetTop;
                dx = geo.client2docX(e.clientX - origX);
                dy = geo.client2docY(e.clientY - origY);
                style.left = geo.client2docX(e.clientX) - dx + "px";
                style.top = geo.client2docY(e.clientY) - dy + "px";
                emg.addEventListener(document, "mousemove", onMouseMoveHandler, false);
                emg.addEventListener(proxy, "mouseup", onMouseUpHandler, false);
                emg.removeEventListener(proxy, "mousedown", onMouseDownHandler, false);
                emg.stopEvent(e);
            };
            var onMouseMoveHandler = function(e){
                style.left = geo.client2docX(e.clientX) - dx + "px";
                style.top = geo.client2docY(e.clientY) - dy + "px";
                emg.stopEvent(e);
            };
            
            var onMouseUpHandler = function(e){
				_this.removeCss("moveable");
                emg.removeEventListener(document, "mousemove", onMouseMoveHandler, false);
                emg.removeEventListener(proxy, "mouseup", onMouseUpHandler, false);
                emg.addEventListener(proxy, "mousedown", onMouseDownHandler, false);
                emg.stopEvent(e);
            }
            emg.addEventListener(proxy, "mousedown", onMouseDownHandler, false);
            this.finalize = function(){
                emg.removeEventListener(proxy, "mousedown", onMouseDownHandler, false);
                emg.removeEventListener(proxy, "mouseup", onMouseUpHandler, false);
                emg.removeEventListener(document, "mousemove", onMouseDownHandler, false);
                this.Super.finalize(this);
            }
        }
    },
    Extends: j2j.patterns.Decorator
});
